﻿using System;
using System.ComponentModel.Design.Serialization;
using System.Collections;
using SedionFormDesigner.Service;
using System.ComponentModel.Design;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Xml.Linq;
using System.ComponentModel;
using SedionFormDesigner.Serializer;

namespace SedionFormDesigner.Designer
{
    /// <summary>
    ///  实现自定义设计器加载程序的基本加载程序
    /// </summary>
    public class SedionDesignerLoader : DesignerLoader
    {
        private IDesignerLoaderHost host;

        private XElement xml = null;

        private Boolean saved = true;
        public Boolean Saved { get { return saved; } }

        public SedionDesignerLoader()
            : this(null) { }

        public SedionDesignerLoader(XElement xml)
        {
            this.xml = xml;
        }

        public override void BeginLoad(IDesignerLoaderHost host)
        {
            if (host == null)
            {
                throw new ArgumentException("host不能为空");
            }

            this.host = host;
            ArrayList errors = new ArrayList();

            // 设计器序列化管理
            host.AddService(typeof(IDesignerSerializationManager), new SedionDesignerSerializationManager(this));
            // 事件绑定
            host.AddService(typeof(IEventBindingService), new SedionEventBindingService(host));
            // 类型加载
            host.AddService(typeof(ITypeResolutionService), new SedionTypeResolutionService());
            // 序列化时的种族文化
            host.AddService(typeof(IResourceService), new SedionResourceService(host));
            // CSharp代码生成器
            host.AddService(typeof(CodeDomProvider), new CSharpCodeProvider());

            String baseClassName = String.Empty;
            if (xml == null)
                baseClassName = host.CreateComponent(typeof(System.Windows.Forms.Form)).Site.Name;
            else
                baseClassName = ReadControl();

            IComponentChangeService changeService = host.GetService(typeof(IComponentChangeService)) as IComponentChangeService;
            if (changeService != null)
            {
                changeService.ComponentChanged += new ComponentChangedEventHandler(OnComponentChanged);
                changeService.ComponentAdded += new ComponentEventHandler(OnComponentAddedRemoved);
                changeService.ComponentRemoved += new ComponentEventHandler(OnComponentAddedRemoved);
            }

            host.EndLoad(baseClassName, true, errors);
        }

        private void OnComponentChanged(object sender, ComponentChangedEventArgs ce)
        {
            saved = false;
        }

        private void OnComponentAddedRemoved(object sender, ComponentEventArgs ce)
        {
            saved = false;
        }

        internal void Save()
        {
            saved = true;
        }

        private String ReadControl()
        {
            return FormSerializer.SerializeXmlToControl(host, xml);
        }

        /// <summary>
        ///  获得加载的主体
        /// </summary>
        public IDesignerLoaderHost LoaderHost
        {
            get
            {
                return host as IDesignerLoaderHost;
            }
        }

        /// <summary>
        ///  根据服务类型获得服务实例
        /// </summary>
        public Object GetService(Type serviceType)
        {
            return host.GetService(serviceType);
        }

        public override void Dispose()
        {
            //throw new NotImplementedException();
        }
    }
}
